package com.example.beson.androiddemo;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hitomi.glideloader.GlideImageLoader;
import com.hitomi.tilibrary.style.progress.ProgressPieIndicator;
import com.hitomi.tilibrary.transfer.TransferConfig;
import com.hitomi.tilibrary.transfer.Transferee;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class GlideNoThumActivity extends AppCompatActivity {
    protected Transferee transferee;
    protected List<String> sourceImageList;
    protected GridView gvImages;
    protected SimpleDraweeView id_simple_drawee_view2,id_simple_drawee_view3,id_simple_drawee_view4;
    protected ImageView gv_glide;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        transferee = Transferee.getDefault(this);
        setContentView(R.layout.activity_glide_no_thum);
        sourceImageList = new ArrayList<>();
        sourceImageList.add("http://i1.17173cdn.com/2fhnvk/YWxqaGBf/cms3/mcofEgbkwpuvlob.jpg!a-3-640x.jpg");
        sourceImageList.add("http://i3.17173cdn.com/2fhnvk/YWxqaGBf/cms3/DWABSHbkCdElEDA.jpg!a-3-640x.jpg");
        sourceImageList.add("http://i2.17173cdn.com/2fhnvk/YWxqaGBf/cms3/xNLzqtbkCdElEyC.jpg!a-3-640x.jpg");
        sourceImageList.add("http://i1.17173cdn.com/2fhnvk/YWxqaGBf/cms3/vUFNHwbljxruktB.jpg!a-3-640x.jpg");
        sourceImageList.add("http://i3.17173cdn.com/2fhnvk/YWxqaGBf/cms3/FNGGjrbljxrukba.jpg!a-3-640x.jpg");
        sourceImageList.add("http://i3.17173cdn.com/2fhnvk/YWxqaGBf/cms3/Juvmrjbkwpuvjyv.jpg!a-3-640x.jpg");
//        sourceImageList.add("http://i1.17173cdn.com/2fhnvk/YWxqaGBf/cms3/qlwPVQbkwpuvmcc.jpg!a-3-640x.jpg");
//        sourceImageList.add("http://i3.17173cdn.com/2fhnvk/YWxqaGBf/cms3/vMbgOlbkwpuvlEE.jpg!a-3-640x.jpg");
//        sourceImageList.add("http://i2.17173cdn.com/2fhnvk/YWxqaGBf/cms3/GJhaLjbkwpuvlty.jpg!a-3-640x.jpg");

        gvImages= (GridView) findViewById(R.id.gv_images);
        gvImages.setAdapter(new NineGridAdapter());
        gv_glide= (ImageView) findViewById(R.id.gv_glide);
        id_simple_drawee_view2= (SimpleDraweeView) findViewById(R.id.id_simple_drawee_view2);
        id_simple_drawee_view3= (SimpleDraweeView) findViewById(R.id.id_simple_drawee_view3);
        id_simple_drawee_view4= (SimpleDraweeView) findViewById(R.id.id_simple_drawee_view4);
        //显示一张HTTP图片保持一定宽高比例，如果4:3(1.33f)，注意xml里的写法
        String url="res://com.example.beson.androiddemo/"+R.mipmap.image3;
//        gv_fresco.setImageURI(Uri.parse("http://img0.bdstatic.com/img/image/cb5016e0d29c198a7b0e7f3dabf7a1351410240928.jpg"));
//        gv_fresco.setAspectRatio(1.33f);
        Glide.with(this).load(R.mipmap.image5).asGif().error( R.drawable.ic_launcher ).into(gv_glide);

        //显示一张HTTP的GIF图片
//        DraweeController draweeController1 = Fresco.newDraweeControllerBuilder().setUri(Uri.parse("http://img0.bdstatic.com/img/image/shitu/feimg/uploading.gif"))
//                .setAutoPlayAnimations(true).build();


        DraweeController draweeController1 = Fresco.newDraweeControllerBuilder().setUri(Uri.parse(url))
                .setAutoPlayAnimations(true).build();


        id_simple_drawee_view2.setController(draweeController1);

        //显示一张HTTP的图片，以圆形图片显示
        id_simple_drawee_view3.setImageURI(Uri.parse("http://img0.bdstatic.com/img/image/44e206d007e8663c8161f386d2cbb9871409804255.jpg"));

        //显示一张HTTP的图片，以圆形带边框图片显示
        id_simple_drawee_view4.setImageURI(Uri.parse("http://img0.bdstatic.com/img/image/77a483d72a2d16264e65858374ab70151409117959.jpg"));




    }
    private class NineGridAdapter extends CommonAdapter<String> {
        public NineGridAdapter() {
            super(GlideNoThumActivity.this, R.layout.item_grid_image, sourceImageList);
        }
        @Override
        protected void convert(ViewHolder viewHolder, String item, final int position) {
            ImageView imageView = viewHolder.getView(R.id.image_view);
            Glide.with(GlideNoThumActivity.this)
                    .load(item)
                    .centerCrop()
                    .placeholder(R.mipmap.ic_launcher)
                    .into(imageView);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TransferConfig config = TransferConfig.build()
                            .setNowThumbnailIndex(position)
                            .setSourceImageList(sourceImageList)
                            .setMissPlaceHolder(R.mipmap.ic_launcher)
                            .setOriginImageList(wrapOriginImageViewList(sourceImageList.size()))
                            .setProgressIndicator(new ProgressPieIndicator())
                            .setImageLoader(GlideImageLoader.with(getApplicationContext()))
                            .create();
                    transferee.apply(config).show(new Transferee.OnTransfereeStateChangeListener() {
                        @Override
                        public void onShow() {
                            Glide.with(GlideNoThumActivity.this).pauseRequests();
                        }

                        @Override
                        public void onDismiss() {
                            Glide.with(GlideNoThumActivity.this).resumeRequests();
                        }
                    });
                }
            });
        }
    }
    protected List<ImageView> wrapOriginImageViewList(int size) {
        List<ImageView> originImgList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            ImageView thumImg = (ImageView) ((LinearLayout) gvImages.getChildAt(i)).getChildAt(0);
            originImgList.add(thumImg);
        }
        return originImgList;
    }
    @Override
    protected void onDestroy() {
        transferee.destroy();

        super.onDestroy();
    }
}
